// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
 
#include <Wire.h>
#include "RTClib.h"

// Defines
#define buzzer 2
#define flowersValve 6
#define grassValve 7
#define manual_A 8
#define manual_B 9

//------------------------------------------------------------
//
//Global vars
//
//------------------------------------------------------------
// RTC
RTC_DS1307 RTC;
// Morning time for watering
DateTime morningWateringTime = DateTime(2014, 1, 1, 20, 25, 0);
// Afternoon time for watering
DateTime afternoonWateringTime = DateTime(2014, 1, 1, 20, 35, 0);
// Morning time for watering
DateTime morningWateringTime_Grass = DateTime(2014, 1, 1, 0, 30, 0);
// Afternoon time for watering
DateTime afternoonWateringTime_Grass = DateTime(2014, 1, 1, 0, 35, 0);
// Watering start time Flowers
DateTime wStartTime_F;
// Watering start time Grass
DateTime wStartTime_G;
// Watering duration
TimeSpan wateringDuration = TimeSpan(0, 0, 1, 0);
// Watering stop
DateTime wStopTime;
// Operation mode A
boolean manualOp_A = false;
// Operation mode B
boolean manualOp_B = false;

//------------------------------------------------------------
//
//Setup
//
//------------------------------------------------------------
void setup () {
    Serial.begin(9600);
    Wire.begin();
    RTC.begin();
 
  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    //RTC.adjust(DateTime(__DATE__, __TIME__));
  }
  // Setup the digital outputs
  pinMode(buzzer, OUTPUT);
  pinMode(flowersValve, OUTPUT);
  pinMode(grassValve, OUTPUT);
  pinMode(manual_A, INPUT);
  pinMode(manual_B, INPUT);
  
  manualOp_A = digitalRead(manual_A);
  manualOp_B = digitalRead(manual_B);
}

//------------------------------------------------------------
//
//Loop
//
//------------------------------------------------------------
void loop () {
    DateTime now = RTC.now();
    
    //Verify Flowers
    Flowers(now);
    
    // Atualiza modo de operação
    manualOp_A = digitalRead(manual_A);
    manualOp_B = digitalRead(manual_B);

    // Datetime.now printing
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
    delay(1000);
}

//------------------------------------------------------------
//
//Tratamento flores
//
//------------------------------------------------------------
void Flowers(DateTime time)
{
  // Manual mode A
    if(manualOp_A && !digitalRead(flowersValve))
    {
      digitalWrite(flowersValve, HIGH);
      Serial.print("Flowers valve ON - MANUAL MODE");
      Serial.println();
    }
    // Automatic mode
    else if(!manualOp_A)
    {
      Serial.print("Automatic Mode");
      Serial.println();
      
      if((((time.hour() == morningWateringTime.hour()) && (time.minute() == morningWateringTime.minute())) || 
        ((time.hour() == afternoonWateringTime.hour()) && (time.minute() == afternoonWateringTime.minute()))) && !digitalRead(flowersValve))
      {
        wStartTime_F = time;
        wStopTime = wStartTime_F + wateringDuration;
        
        Serial.print("Start watering time: ");
        Serial.print(wStartTime_F.hour(), DEC);
        Serial.print(':');
        Serial.print(wStartTime_F.minute(), DEC);
        Serial.println();
        
        Serial.print("Stop watering time: ");
        Serial.print(wStopTime.hour(), DEC);
        Serial.print(':');
        Serial.print(wStopTime.minute(), DEC);
        Serial.println();
        
        digitalWrite(flowersValve, HIGH);
        digitalWrite(buzzer, HIGH);
        Serial.print("Flowers valve ON - AUTOMATIC MODE");
        Serial.println();
      }
      else if((wStopTime.unixtime() < time.unixtime()) && digitalRead(flowersValve))
      {
        digitalWrite(flowersValve, LOW);
        digitalWrite(buzzer, LOW);
        Serial.print("Flowers valve OFF ");
        Serial.println();
      }
    }
}
